//
//  OMHomeAddressSelectViewController.m
//  OMENGO_NEW
//
//  Created by 梁尚嘉 on 2017/4/24.
//  Copyright © 2017年 Kamfat. All rights reserved.
//

#import "OMHomeAddressSelectViewController.h"
#import "OMOriginalAddress.h"
#import "KTElementTableCell.h"
@interface OMHomeAddressSelectViewController ()
@property (nonatomic, strong) NSArray <OMOriginalAddress*>*addresses;
@end

@implementation OMHomeAddressSelectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.title = @"请选择楼盘的地址";
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    [self getProject];
}

- (void)reloadDataFromNetWork
{
    [self getProject];
}

- (void)getProject
{
    [self startAnimating:true];
    [KTHttpUtils GET:@"index/project.jhtml"
          completion:^(id responseObject,
                       BOOL isSuccess,
                       NSString *message)
    {
        [self startAnimating:false];
        if (isSuccess) {
            _addresses = [OMOriginalAddress mj_objectArrayWithKeyValuesArray:responseObject[OMDataKey]];
            [self.tableView reloadData];
        }else {
            [self showNetWorkError];
        }
    }];
}


#pragma mark - Tableview delegate && datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _addresses.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    KTElementTableCell *cell = [KTElementTableCell tableViewCellWithTableView:tableView];
    cell.elementLabel.text = _addresses[indexPath.row].name;
    cell.canHighLight = true;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [KTElementTableCell cellHeight];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.navigationController.viewControllers.count > 1 ) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(ctrl:didSelectObject:)]) {
            [self.delegate ctrl:self didSelectObject:self.addresses[indexPath.row]];
            NaviPopAnimated(true);
        }
    }else {
        KTSystemStorageSingleton.originalAddress = _addresses[indexPath.row];
    }
}


@end
